package com.xilihui.xlh.business.activitys.store;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xilihui.xlh.R;
import com.xilihui.xlh.alipay.AuthResult;
import com.xilihui.xlh.alipay.PayResult;
import com.xilihui.xlh.business.dialogs.PayDialog;
import com.xilihui.xlh.business.entities.PayEntity;
import com.xilihui.xlh.business.entities.StoreGoodDetailsEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;
import com.xilihui.xlh.wxapi.WXPayEntryActivity;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/18.
 */

public class OrderDetailsActivity extends BaseCompatActivity{
    
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_syTime)
    TextView tv_syTime;
    @BindView(R.id.tv_bianhao)
    TextView tv_bianhao;
    @BindView(R.id.tv_createTime)
    TextView tv_createTime;
    @BindView(R.id.tv_payTime)
    TextView tv_payTime;
    @BindView(R.id.tv_fahuoTime)
    TextView tv_fahuoTime;
    @BindView(R.id.tv_makeTime)
    TextView tv_makeTime;
    
    @BindView(R.id.ll_payTime)
    LinearLayout ll_payTime;
    @BindView(R.id.ll_fahuoTime)
    LinearLayout ll_fahuoTime;
    @BindView(R.id.ll_makeTime)
    LinearLayout ll_makeTime;
    
    @BindView(R.id.tv_hint_pay)
    TextView tv_hint_pay;
    
    @BindView(R.id.rl_buttom)
    ConstraintLayout rl_button;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_pay)
    TextView tv_pay;
    @BindView(R.id.tv_link)
    TextView tv_link;
    
    ArrayList<StoreGoodDetailsEntity.OrderInfoBean.OrderBean> datas = new ArrayList<>();
    BaseAdapter<StoreGoodDetailsEntity.OrderInfoBean.OrderBean> baseAdapter;
    
    int type = 0;// 1 未付款订单 2.其他订单
    String order_all_id = "";
    String order_id = "";
    String sign_sn = "";
    
    int status = 0;// 0(待付款) 1(待发货) 2(待收货) 3(待评价)
    int cancel_type = 0;
    
    boolean isQRcode = false;//是否自提
    
    String refund_status = "";//退款 状态
    
    String image = "";
    
    String jiner = "";
    String jifen = "";
    
    String phone = "";//联系客服
    
    StoreGoodDetailsEntity storeGoodDetailsEntity;

    public static String orderInfo = "";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    Intent intent;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        ToastUtil.toastShortPositive("支付成功");
                        YEventBuses.post(new YEventBuses.Event("pay"));
                        Intent intent = new Intent(OrderDetailsActivity.this, TradeSuccessActivity.class);
                        intent.putExtra("type",2);
                        intent.putExtra("jiner",jiner);
                        intent.putExtra("jifen",jifen);
                        startActivity(intent);
                        finish();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastUtil.toastShortNegative("支付失败");
                        YEventBuses.post(new YEventBuses.Event("pay"));
                        
                    }
//                    intent = new Intent(ConfirmListingActivity.this,MyChecklistActivity.class);
//                    intent.putExtra("wa_id",wa_id);
//                    intent.putExtra("agent_id",agent_id);
//                    startActivity(intent);
//                    YEventBuses.post(new YEventBuses.Event("pay"));
//                    finish();
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        ToastUtil.toastShortNegative("\"授权成功\\n\" + String.format(\"authCode:%s\", authResult.getAuthCode())");
                    } else {
                        // 其他状态值则为授权失败
                        ToastUtil.toastShortNegative("授权失败" + String.format("authCode:%s", authResult.getAuthCode()));
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };
    
    @Override
    public int getLayout() {
        return R.layout.activity_store_order_details;
    }

    @Override
    public String initTitle() {
        return "订单详情";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type",1);
        status = getIntent().getIntExtra("status",0);
        order_all_id = getIntent().getStringExtra("order_all_id");
        order_id = getIntent().getStringExtra("order_id");
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen._7dp, LinearLayout.VERTICAL));
        
        baseAdapter = new BaseAdapter<StoreGoodDetailsEntity.OrderInfoBean.OrderBean>(this,datas) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_store_order_details;
            }

            @Override
            protected void getView(BaseViewHolder holder, final StoreGoodDetailsEntity.OrderInfoBean.OrderBean entity, int position) {
                
                holder.setText(R.id.tv_shopName,"收件人："+entity.getConsignee());
                TextView textView = (TextView)holder.getView(R.id.tv_phone);
                textView.setCompoundDrawables(null,null,null,null);
                
                holder.setText(R.id.tv_address,"收货地址："+entity.getProvince().getName()+
                entity.getCity().getName());
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_good_img);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOrder_goods().getOriginal_img());
                holder.setText(R.id.tv_name,entity.getOrder_goods().getGoods_name());
                holder.setText(R.id.tv_model,entity.getOrder_goods().getSpec_key_name());
                holder.setText(R.id.tv_place,"￥"+entity.getOrder_goods().getGoods_price());
                holder.setText(R.id.tv_totalNum,"x"+entity.getOrder_goods().getGoods_num());
                
                holder.setText(R.id.tv_total_price,"￥"+entity.getTotal_amount());
                holder.setText(R.id.tv_yunfei_price,"包邮");
                holder.setText(R.id.tv_jifeng_price,"￥"+entity.getIntegral_money());
                holder.setText(R.id.tv_sf_price,"￥"+entity.getOrder_amount());
                holder.setText(R.id.tv_fangjifeng,"返积分"+entity.getOrder_goods().getGive_integral()+"分");
                
                holder.setVisibility(R.id.tv_tuikuang,View.VISIBLE);
                
                LogUtil.i("mylog","status:"+status);
                
                if(entity.getShipping_code().equals("ziti")){
                    holder.setVisibility(R.id.tv_fjmd,View.VISIBLE);
                    holder.setOnClickListener(R.id.tv_fjmd, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(OrderDetailsActivity.this,NearbyStoresActivity.class);
                            intent.putExtra("agent_id",entity.getAgent_id());
                            startActivity(intent);
                        }
                    });
                    
                    if(status==2){
                        
                        holder.setVisibility(R.id.tv_tuikuang,View.VISIBLE);
                        holder.setText(R.id.tv_tuikuang,"退款");
                        holder.setOnClickListener(R.id.tv_tuikuang, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(refund_status.equals("0")){
                                    toAlteration();
                                }else {
                                    toAlterationStatus();
                                }
                            }
                        });
                        
                    }else if(status==3){
                        holder.setVisibility(R.id.tv_tuikuang,View.VISIBLE);
                        if(isQRcode){
                            holder.setText(R.id.tv_tuikuang,"退款码");
                            holder.setOnClickListener(R.id.tv_tuikuang, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                                 
                                }
                            });
                        }else {
                            holder.setText(R.id.tv_tuikuang,"退款");
                            holder.setOnClickListener(R.id.tv_tuikuang, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if(refund_status.equals("0")){
                                        toAlteration();
                                    }else {
                                        toAlterationStatus();
                                    }
                                }
                            });
                        }
                    }else {
                        holder.setVisibility(R.id.tv_tuikuang,View.GONE);
                    }
                    LogUtil.i("mylog","自提");
                }else {
                    holder.setVisibility(R.id.tv_fjmd,View.GONE);
                    if(status==0){
                        holder.setVisibility(R.id.tv_tuikuang,View.GONE);
                    }else if(status==1||status==2){
                        holder.setVisibility(R.id.tv_tuikuang,View.VISIBLE);
                        holder.setText(R.id.tv_tuikuang,"退款");
                        holder.setOnClickListener(R.id.tv_tuikuang, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(refund_status.equals("0")){
                                    toAlteration();
                                }else {
                                    toAlterationStatus();
                                }
                            }
                        });
                    }else if(status==3){
                        holder.setVisibility(R.id.tv_tuikuang,View.VISIBLE);
                        holder.setText(R.id.tv_tuikuang,"退换");
                        holder.setOnClickListener(R.id.tv_tuikuang, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(refund_status.equals("0")){
                                    toAlteration();
                                }else {
                                    toAlterationStatus();
                                }
                            }
                        });
                    }
                    LogUtil.i("mylog","快递包邮");
                }
                                    
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        
    }

    @Override
    public void initData() {
        getOrderAll();
    }
    
    
    public void getOrderAll(){
        
        if(TextUtil.isEmpty(order_id)){
            order_id = "";
        }
        
        if(TextUtil.isEmpty(order_all_id)){
            order_all_id = "";
        }
        LogUtil.i("mylog","order_id"+order_id+"order_all_id"+order_all_id);
        StoreRequest.orderAllInfo(OrderDetailsActivity.this,order_id,order_all_id)
                .compose(DoTransform.<StoreGoodDetailsEntity>applyScheduler(this,true))
                .compose(this.<StoreGoodDetailsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreGoodDetailsEntity>(this) {
                    @Override
                    protected void onSuccess(StoreGoodDetailsEntity entity) {
                        
                        refund_status = entity.getOrder_info().getOrder().get(0).getOrder_refund_status();
                        LogUtil.i("mylog","getdata");
                        phone = entity.getPhone();
                        
                        storeGoodDetailsEntity = entity;
                        sign_sn = entity.getOrder_info().getOrder_sn();
                        
                        tv_status.setText(entity.getOrder_info().getTitle());
                        tv_syTime.setText(entity.getOrder_info().getSubtitle());
                        
                        tv_bianhao.setText(entity.getOrder_info().getOrder_sn());
                        tv_createTime.setText(entity.getOrder_info().getOrder().get(0).getAdd_time());
                        
                        String order_statusTitle = entity.getOrder_info().getTitle();
                        if(order_statusTitle.equals("待付款")){
//                            tv_status.setText("待付款");
                            ll_payTime.setVisibility(View.GONE);
                            ll_fahuoTime.setVisibility(View.GONE);
                            ll_makeTime.setVisibility(View.INVISIBLE);
                            
                            cancel_type = 0;
                            
                        }else if(order_statusTitle.equals("待发货")){
//                            tv_status.setText("待发货");
                            tv_payTime.setText(entity.getOrder_info().getOrder().get(0).getPay_time());
                            ll_fahuoTime.setVisibility(View.GONE);
                            ll_makeTime.setVisibility(View.INVISIBLE);
                            rl_button.setVisibility(View.GONE);
                            
                        }else if(order_statusTitle.equals("待收货")){
//                            tv_status.setText("待收货");
                            tv_payTime.setText(entity.getOrder_info().getOrder().get(0).getPay_time());
                            tv_fahuoTime.setText(entity.getOrder_info().getOrder().get(0).getShipping_time());
                            ll_makeTime.setVisibility(View.INVISIBLE);
                            
                            if(entity.getOrder_info().getOrder().get(0).getShipping_code().equals("ziti")){
                                isQRcode = true;
                                tv_cancel.setText("领货码");
                                ll_fahuoTime.setVisibility(View.GONE);
                            }else {
                                isQRcode = false;
                                tv_cancel.setText("确认收货");
                            }
                            
                            tv_pay.setVisibility(View.GONE);
                            cancel_type = 1;
                            
                        }else if(order_statusTitle.equals("交易成功")){
//                            tv_status.setText("交易成功");
                            cancel_type = 2;
                            if(entity.getOrder_info().getType_status().equals("dpj")){
                                tv_pay.setText("评价");
                                tv_cancel.setText("删除订单");
                            }else {
                                tv_pay.setVisibility(View.GONE);
                                tv_cancel.setText("删除订单");
                            }
                            
                            if(entity.getOrder_info().getOrder().get(0).getShipping_code().equals("ziti")){
                                isQRcode = true;
                            }else {
                                isQRcode = false;   
                            }
                            
                            tv_payTime.setText(entity.getOrder_info().getOrder().get(0).getPay_time());
                            tv_fahuoTime.setText(entity.getOrder_info().getOrder().get(0).getShipping_time());
                            tv_makeTime.setText(entity.getOrder_info().getOrder().get(0).getConfirm_time());
                        }else if(order_statusTitle.equals("已取消")){
                            tv_status.setText("已取消");
//                            tv_hint_pay.setText("取消时间");
                            tv_payTime.setText(entity.getOrder_info().getOrder().get(0).getCancel_time());
                            ll_fahuoTime.setVisibility(View.GONE);
                            ll_makeTime.setVisibility(View.INVISIBLE);
                        }else if(order_statusTitle.equals("系统取消")){
//                            tv_status.setText("系统取消");
                            tv_hint_pay.setText("取消时间");
                            tv_payTime.setText(entity.getOrder_info().getOrder().get(0).getCancel_time());
                            ll_fahuoTime.setVisibility(View.GONE);
                            ll_makeTime.setVisibility(View.INVISIBLE);
                        }

                        datas.addAll(entity.getOrder_info().getOrder());
                        baseAdapter.setList(datas);
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortPositive(msg);
                    }
                });
        
    }

    /**
     * 联系客服
     */
    @OnClick(R.id.tv_link)
    public void link(){

        
        
    }

    /**
     * 取消订单
     */
    @OnClick(R.id.tv_cancel)
    public void cancel(){
        
        switch (cancel_type){
            case 0://取消订单
                
                StoreRequest.cancelOrder(OrderDetailsActivity.this,order_all_id)
                        .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                        .compose(this.<BaseEntity>bindToLifecycle())
                        .subscribe(new HttpSubscriber<BaseEntity>(this) {
                            @Override
                            protected void onSuccess(BaseEntity entity) {
                                ToastUtil.toastShortPositive(entity.getMsg());
                                YEventBuses.post(new YEventBuses.Event("store_order_cancel"));
                                finish();
                            }

                            @Override
                            protected void onFail(String msg) {
                                ToastUtil.toastShortNegative(msg);
                            }
                        });
                                
                break;
            case 1://确认收货
                
                if(!isQRcode){
                    StoreRequest.confirmReceipt(OrderDetailsActivity.this,order_id)
                            .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                            .compose(this.<BaseEntity>bindToLifecycle())
                            .subscribe(new HttpSubscriber<BaseEntity>(this) {
                                @Override
                                protected void onSuccess(BaseEntity entity) {
//                                            ToastUtil.toastShortPositive(entity.getMsg());
                                    YEventBuses.post(new YEventBuses.Event("querenshouhuo"));
                                    Intent intent = new Intent(OrderDetailsActivity.this, TradeSuccessActivity.class);
                                    intent.putExtra("type",1);
                                    startActivity(intent);
                                    finish();
                                }
                                
                                @Override
                                protected void onFail(String msg) {
                                    ToastUtil.toastShortNegative(msg);
                                }
                            });
                }else {
                    Intent intent = new Intent(OrderDetailsActivity.this,QRcodeActivity.class);
                    intent.putExtra("order_id",order_id);
                    startActivity(intent);
                }
                
                break;
            case 2://删除订单
                
                StoreRequest.deleteOrder(this,order_id)
                        .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                        .compose(this.<BaseEntity>bindToLifecycle())
                        .subscribe(new HttpSubscriber<BaseEntity>(this) {
                            @Override
                            protected void onSuccess(BaseEntity entity) {
                                ToastUtil.toastLongPositive(entity.getMsg());
                                YEventBuses.post(new YEventBuses.Event("delete_cancel"));
                                finish();
                            }
                                            
                            @Override
                            protected void onFail(String msg) {
                                ToastUtil.toastShortNegative(msg);
                            }
                        });
                
                break;
        }
        
        
    }

    /**
     * 去付款
     */
    @OnClick(R.id.tv_pay)
    public void pay(){
        
        switch (cancel_type){
            case 0://付款
                PayDialog payDialog = new PayDialog(OrderDetailsActivity.this);
                payDialog.setOnItemSelectListener(new PayDialog.OnItemSelectListener() {
                    @Override
                    public void close() {

                    }

                    @Override
                    public void alipay() {
                        YEventBuses.post(new YEventBuses.Event("order_details_pay").setParams("alipay"));
                    }

                    @Override
                    public void weixin() {
                        YEventBuses.post(new YEventBuses.Event("order_details_pay").setParams("weixin"));
                    }
                });
                payDialog.show();
                break;
            case 2://评价
                
                Intent intent = new Intent(this, PostEvaluationActivity.class);
                intent.putExtra("order_id",order_id);
                intent.putExtra("image",storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getOrder_goods().getOriginal_img());
                startActivity(intent);
                         
                break;
        }
        
    }

    public void pay(final String sign_sn, final String code){
        StoreRequest.pay(OrderDetailsActivity.this,sign_sn,code)
                .compose(DoTransform.<PayEntity>applyScheduler(OrderDetailsActivity.this,true))
                .compose(this.<PayEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<PayEntity>(OrderDetailsActivity.this) {
                    @Override
                    protected void onSuccess(PayEntity ent) {
                        jiner = ent.getOrder_info().getOrder_price();
                        jifen = ent.getOrder_info().getOrder_all_give_money();
                        if(code.equals("alipay")){
                            payV2(recyclerView);
                        }else {
                            weixiPay(ent);
                        }
                    }
                    
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("order_details_pay")){
            String type = (String) event.getParams(0);
            if(type.equals("alipay")){
                pay(sign_sn,"alipay");
            }else {
                pay(sign_sn,"weixin");
            }
        }else if(event.is("pay")){
            finish();
        }else if(event.is("yipingjia")){
            finish();
        }else if(event.is("yifukuang")){
            finish();
        }else if(event.is(StrConst.UMENG_PUSH)){
            if(event.getParams(0).equals("8")){
                finish();
            }
        }else if(event.is("revocation")){//撤销退款成功
            finish();
        }
        
    }
    
    
    /**
     * 支付宝支付业务
     *
     * @param v
     */
    public void payV2(View v) {
//        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);

//        /**
//         * 这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
//         * 真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
//         * 防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险； 
//         *
//         * orderInfo的获取必须来自服务端；
//         */
//        boolean rsa2 = true;
//        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
//          
//        
//
//        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
//        final String orderInfo = orderParam + "&" + sign;

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(OrderDetailsActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
        
    }
    
    public void weixiPay(PayEntity payEntity){
            
        SPUtil.put(OrderDetailsActivity.this,SPUtil.PAY_TYPE,2);
        SPUtil.put(OrderDetailsActivity.this,"jiner",jiner);
        SPUtil.put(OrderDetailsActivity.this,"jifen",jifen);
        
        IWXAPI msgApi = WXAPIFactory.createWXAPI(OrderDetailsActivity.this,"wx086831d3499ea9c2", true);
        msgApi.registerApp("wx086831d3499ea9c2");
        
        boolean isWXAppInstalled = msgApi .isWXAppInstalled();
        if(isWXAppInstalled){
            PayReq request = new PayReq();
            request.appId = payEntity.getWeixin_payment_params().getAppid();
            request.partnerId = payEntity.getWeixin_payment_params().getPartnerid();
            request.prepayId= payEntity.getWeixin_payment_params().getPrepayid();
            request.packageValue = "Sign=WXPay";
            request.nonceStr= payEntity.getWeixin_payment_params().getNoncestr();
            request.timeStamp= payEntity.getWeixin_payment_params().getTimestamp()+"";
            request.sign= payEntity.getWeixin_payment_params().getSign();
            msgApi.sendReq(request);
        }else {
            ToastUtil.toastShortPositive("请先安装微信");
        }
        
    }
    
    @OnClick(R.id.tv_link)
    public void service(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }

    /**
     * 申请退款
     */
    public void toAlteration(){
        Intent intent = new Intent(OrderDetailsActivity.this,AlterationActivity.class);
        intent.putExtra("order_id",order_id);
        intent.putExtra("status",refund_status);
        intent.putExtra("data",storeGoodDetailsEntity);
        startActivity(intent);
    }

    /**
     * 退款状态
     */
    public void toAlterationStatus(){
        Intent intent = new Intent(OrderDetailsActivity.this,AlterationStatusActivity.class);
        intent.putExtra("order_id",order_id);
        intent.putExtra("status",status);
        startActivity(intent);
    }
    
    
}
